package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 菜品管理
@Service
@Transactional
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    // 分页条件查询
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        // 1.构建菜品条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Dish::getName, name);

        // 2.构建分页
        Page<Dish> page = new Page<>(pageNum, pageSize);

        // 3.查询分页
        page = dishMapper.selectPage(page, qw);

        // 4.遍历菜品list集合
        List<Dish> dishList = page.getRecords();
        for (Dish dish : dishList) {
            // 4-1 查询口味
            // a) 构建口味条件
            LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
            dqw.eq(DishFlavor::getDishId, dish.getId());
            // b）查询口味列表
            List<DishFlavor> flavorList = dishFlavorMapper.selectList(dqw);
            // c）封装到菜品实体中
            dish.setFlavors(flavorList);
            // 4-2 查询分类
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());
        }
        // 5.返回分页对象
        return page;
    }

    @Override
    public void save(Dish dish) {
        //添加基本数据
        dishMapper.insert(dish);
        //遍历口味
        List<DishFlavor> flavors = dish.getFlavors();
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(dish.getId());
            dishFlavorMapper.insert(dishFlavor);
        }
    }

    @Override
    public Dish findByid(Long id) {
        //遍历基本信息
        Dish dish = dishMapper.selectById(id);
        //构建条件
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, id);
        //调用查询并存入数据库
        dish.setFlavors(dishFlavorMapper.selectList(qw));
        return dish;
    }

    @Override
    public void update(Dish dish) {
        //修改基本信息
        dishMapper.updateById(dish);
        //删除原本口味
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(qw);
        //添加新口味
        List<DishFlavor> flavors = dish.getFlavors();
        for (DishFlavor dishFlavor : flavors) {
            //补全信息
            dishFlavor.setDishId(dish.getId());
            dishFlavorMapper.insert(dishFlavor);
        }
    }

    @Override
    public void updateStatus(Integer status,  List<Long> ids) {
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId, ids);
        Dish dish = new Dish();
        dish.setStatus(status);
        dishMapper.update(dish,qw);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        // 1.先查询套餐状态，是否有启售 select count(*) from setmeal where status=1 and id in(1,3,5);
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId, ids);
        qw.eq(Dish::getStatus, 1);
        Integer count = dishMapper.selectCount(qw);
        if (count > 0) {
            throw new CustomException("套餐中有启售状态，请先停售再删除~~~");
        }
        // 2.删菜品
        dishMapper.deleteBatchIds(ids);
        //3.删除套餐内菜品中间表
        LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
        dqw.in(DishFlavor::getDishId, ids);
        dishFlavorMapper.delete(dqw);
    }

    @Override
    public List<Dish> findListBySetmeal(Long categoryId, String name) {
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Dish::getName, name);
        qw.eq(categoryId != null,Dish::getCategoryId, categoryId);
        return dishMapper.selectList(qw);
    }

    @Override
    public List<Dish> findList(Long categoryId, Integer status) {
        //查询
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId, categoryId);
        qw.eq(Dish::getStatus, 1);
        return dishMapper.selectList(qw);
    }

}
